In [1]:
import pip_magic
from playlist_maker import PlaylistMaker
from IPython.display import Image, display, HTML
import math
import toolz
import pandas as pd
# import seaborn as sns
import matplotlib.pyplot as plt
In [2]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
# Note: pip_magic does not work with pip v10. I used pip v9.0.3
%pip install spotipy
In [3]:
playlist = PlaylistMaker()
In [4]:
def image_grid(images, rows=1, height=200, width=200):
images_as_html = '<table>'
images_per_row = math.ceil(len(images) / rows)
for r in range(rows):
images_this_row = images[r*images_per_row : r*images_per_row+images_per_row]
if images_this_row:
images_as_html += '<tr>'
for img in images_this_row:
images_as_html += f"<td><img src='{img}' height='{height}' width='{width}''></td>"
images_as_html += '</tr>'
images_as_html += '</table>'
display(HTML(images_as_html))
In [5]:
artists = ['David Ramirez', 'Jeffrey Foucault', 'John Fullbright', 'Joe Purdy']
artist_deets = playlist.artist_details(artists).set_index('Artist Name')
In [6]:
most_followers = artist_deets.sort_values('Artist Followers',ascending=False)['Artist Followers'][:1].to_dict()
artist_, followers_ = list(most_followers.items())[0]
print(f'Out of {", ".join(artists)}...')
print(f'{artist_} has the most followers with {followers_:,}.')
In [7]:
related_artists = (playlist.find_related_artists(playlist.artist_details(artists), 3)
.set_index('Artist Name')
.sort_values('Artist Followers', ascending=False))
print('Here are the top 3 related artists for each of your artists:')
display(related_artists[['Artist Followers', 'Artist Popularity']])
imgs = [related_artists.at[a, 'Artist Image'] for a in related_artists.index]
image_grid(imgs,3,300,300);
In [8]:
genres = list(related_artists['Artist Genres'])
genres = sum(genres, []) # slick way to flatten list
genres_occurance = toolz.frequencies(genres)
print(f'Out of the {len(related_artists)} related artists, here are the most common genres:')
for g, v in pd.Series(genres_occurance).sort_values(ascending=False)[:15].iteritems():
print(f' {g} ({v})')
In [9]:
artist = 'The Beatles'
artist_tracks = playlist.get_artist_tracks(artist)
artist_tracks = playlist.add_audio_features(artist_tracks)
print(f'{len(artist_tracks)} tracks from {artist}.')
artist_tracks['Release Date'] = pd.to_datetime(artist_tracks['Release Date'])
artist_tracks = (artist_tracks.set_index(['Album Name','Track Name'])
.sort_values('Track Popularity',ascending=False))
artist_tracks[:5].drop(['Album ID', 'Artist', 'Track ID'],axis=1)
Out[9]:
In [10]:
x_var, y_var, c_var = ('Danceability', 'Energy', 'Valence')
f, ax = plt.subplots(figsize=(10,6))
x=artist_tracks[x_var]
y=artist_tracks[y_var]
c=artist_tracks[c_var]
ax.set_xlabel(x_var)
ax.set_ylabel(y_var)
ax.set_xlim((0,1))
ax.set_ylim((0,1))
ax.set_title('Details about Every Beatles Song')
points = ax.scatter(x, y, c=c, s=70, cmap='BrBG')
f.colorbar(points, label=c_var);
In [11]:
artist_tracks_grouped = artist_tracks.groupby('Album Name').mean()
In [12]:
print('Average Scores for all Beatles Albums')
artist_tracks_grouped.sort_values('Danceability', ascending=False)
Out[12]:
In [13]:
ax = artist_tracks_grouped.sort_values('Track Popularity', ascending=False)[1:7][['Acousticness', 'Danceability','Energy', 'Instrumentalness',
'Liveness', 'Valence']].T.plot(kind='barh', figsize=(12,8))
ax.set_title('Beatles Album Details')
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), shadow=True, ncol=2);
In [14]:
artist = 'Tom Waits'
tracks = playlist.get_artist_tracks(artist)
tracks = playlist.add_audio_features(tracks)
x_var, y_var, c_var = ('Energy', 'Acousticness', 'Valence')
f, ax = plt.subplots(figsize=(12,8))
x=tracks[x_var]
y=tracks[y_var]
c=tracks[c_var]
ax.set_xlabel(x_var)
ax.set_ylabel(y_var)
ax.set_xlim((0,1))
ax.set_ylim((0,1))
ax.set_title(f'{artist} Catalog - Song Details')
points = ax.scatter(x, y, c=c, s=70, cmap='BrBG')
f.colorbar(points, label=c_var)
print(f'{len(tracks)} tracks from {artist}.')
In [15]:
tracks = playlist.track_details_from_playlist('Special Earth Songs from Tennessee')
plylst_tracks = playlist.add_audio_features(tracks)
x_var, y_var, c_var = ('Energy', 'Acousticness', 'Valence')
f, ax = plt.subplots(figsize=(12,8))
x=plylst_tracks[x_var]
y=plylst_tracks[y_var]
c=plylst_tracks[c_var]
ax.set_xlabel(x_var)
ax.set_ylabel(y_var)
ax.set_xlim((0,1))
ax.set_ylim((0,1))
ax.set_title('Special Earth Songs from Tennessee Playlist Song Details')
points = ax.scatter(x, y, c=c, s=70, cmap='BrBG')
f.colorbar(points, label=c_var);
In [16]:
artists = ['David Ramirez', 'Jason Isbell', 'John Fullbright', 'Ryan Adams']
tracks = playlist.create_track_list_of_related_artists(artists, include_seed_artists=True,
num_top_tracks_per_artist=5,
num_related_artists=8)
plylst_tracks = playlist.add_audio_features(tracks).sort_values('Track Popularity',ascending=False)
print(f'Based on the {len(artists)} seed artists, a playlist of {len(plylst_tracks)} songs was created.')
In [17]:
related_artists = (plylst_tracks[['Artist Followers', 'Artist Image', 'Artist Name', 'Artist Popularity']]
.drop_duplicates().set_index('Artist Name').dropna()
.sort_values('Artist Followers',ascending=False))
print('Artists:',', '.join(list(related_artists.index)))
imgs = [related_artists.at[a, 'Artist Image'] for a in related_artists.index]
image_grid(imgs,5,300,300);
In [18]:
playlist.create_playlist_of_tracks(plylst_tracks,'Jupy Sunday')
In [29]:
artists = ['LCD Soundsystem', 'Talking Heads', 'Muse', 'Arcade Fire', 'Radiohead']
tracks = playlist.create_track_list_of_related_artists(artists, include_seed_artists=True,
num_top_tracks_per_artist=5,
num_related_artists=5)
plylst_tracks = playlist.add_audio_features(tracks).sort_values('Track Popularity',ascending=False)
print(f'Based on the {len(artists)} seed artists, a playlist of {len(plylst_tracks)} songs was created.')
In [20]:
related_artists = (plylst_tracks[['Artist Followers', 'Artist Image', 'Artist Name', 'Artist Popularity']]
.drop_duplicates().set_index('Artist Name').dropna()
.sort_values('Artist Followers',ascending=False))
print('Artists:',', '.join(list(related_artists.index)))
imgs = [related_artists.at[a, 'Artist Image'] for a in related_artists.index]
image_grid(imgs,5,300,300);
In [22]:
playlist.create_playlist_of_tracks(plylst_tracks,'Jupy Sunday 2')
In [23]:
# 'Acousticness', 'Danceability', 'Duration',
# 'Energy', 'Instrumentalness', 'Key', 'Liveness', 'Loudness',
# 'Speechiness', 'Valence'
artists=['LCD Soundsystem','Talking Heads', 'Muse', 'Arcade Fire', 'Radiohead']
tracks = playlist.get_recommendations(seed_artists=artists, country='US',
min_energy=0.5, max_liveness=0.4, limit=100)
plylst_tracks = playlist.add_audio_features(tracks)
plylst_tracks = plylst_tracks.set_index('Track Name')
In [28]:
plylst_tracks = plylst_tracks.sort_values('Energy',ascending=True)
playlist.create_playlist_of_tracks(plylst_tracks,'Jupy Sunday 3')
print(f'Based on the {len(artists)} seed artists, a playlist of {len(plylst_tracks)} songs was created.')
print('Artists:',', '.join(set(list(plylst_tracks['Artist'].values))))
imgs = list(plylst_tracks['Album URL'].values)
image_grid(imgs,14,300,300);
In [25]:
In [ ]: